package mediatheque.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

public class LoginDialog extends JDialog {


	private JLabel nomLabel, passLabel;
	private JTextField nom;
	private JPasswordField pass;
	private LoginData data;
	private static final long serialVersionUID = -6712115683786406685L;

	public LoginDialog(JFrame parent, String title, boolean modal){
		super(parent, title, modal);
		this.setSize(220, 150);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		initComponent();
	}
	

	private void initComponent(){
		//Le nom
		JPanel panNom = new JPanel();
		panNom.setPreferredSize(new Dimension(220, 30));
		nom = new JTextField();
		nom.setPreferredSize(new Dimension(100, 25));
		nomLabel = new JLabel("Login :");
		nomLabel.setPreferredSize(new Dimension(100, 25));
		panNom.add(nomLabel);		
		panNom.add(nom);
		
		
		JPanel panPass = new JPanel();
		panPass.setPreferredSize(new Dimension(220, 30));
		pass = new JPasswordField();
		pass.setPreferredSize(new Dimension(100, 25));
		passLabel = new JLabel("Password :");
		passLabel.setPreferredSize(new Dimension(100, 25));
		panPass.add(passLabel);
		panPass.add(pass);
			
		
		JPanel control = new JPanel();
		JButton okBouton = new JButton("Se connecter");
		
		okBouton.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {	
				data = new LoginData(nom.getText(), pass.getText().trim());
				setVisible(false);
			}			
		});
		
		JButton cancelBouton = new JButton("Fermer");
		cancelBouton.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				setVisible(false);
			}			
		});
		
		control.add(okBouton);
		control.add(cancelBouton);
		
		JPanel content = new JPanel();
		content.add(panNom);
		content.add(panPass);
		
		getRootPane().setDefaultButton(okBouton);
		
		this.getContentPane().add(content, BorderLayout.CENTER);
		this.getContentPane().add(control, BorderLayout.SOUTH);
	}
	
	public LoginData showLoginDialog(){
		this.setVisible(true);		
		return this.data;		
	}
	

}
